Image
to PNGBufferedImage
to PNGWritten by J. David Eisenberg (home page)
version 1.5 (see change log)
The PngEncoder
class takes a Java Image
as its
input and produces a byte array in PNG format. This array can be saved in
a file or sent to a client by a servlet.
This functionality is now included in the
javax.imageio
library that comes with Java 1.4.1,
but PngEncoder may be useful for those who don’t want to require the
latest version of Java.
The source Image
is presumed to be based on the Java
DirectColorModel
; that is, pixels are four-byte int
s
containing alpha channel, red, green, and blue information.
Thus, this PNG encoder creates PNGs with a color type of 2 (that is,
RGB triples) and a bit depth of 8, optionally encoding
alpha channel information.
This class does not work with
an IndexColorModel
image.
Image data may be pre-filtered using the “sub” (previous byte) and “up” (previous row) filters.
You may also set the compression level (0 through 9) as used in java.utils.zip.Deflater
The PngEncoderB
class has been designed to allow encoding
of images that are based on an indexed color model with a palette.
PngEncoderB
takes a Java BufferedImage
as its
input and produces a corresponding set of PNG bytes. Because it
uses a BufferedImage
, this class requires Java 1.2.
PngEncoder
You may see a sample program which
draws an clock face and saves it in an appropriately named file.
That is, if you use the program at 3:24 PM, and specify filter
two with a compression level of 5, it will display a
clock face and save that same image in a file named
clock1524_f25.png
.
Its usage is as follows:
Usage: TestEncoder -alpha -filter n -compress c alpha means to use alpha encoding (default none) n is filter number 0=none (default), 1=sub, 2=up c is compression factor (0-9)
PngEncoderB
This sample program also
draws an clock face and saves it in an appropriately named file.
The file name is constructed as in the example above, except
that the name ends in d
if the image is 24-bit (direct
color) and i
if 8-bit (indexed color). An example
file name might be clock1524_f25d.png
.
Its usage is as follows:
Usage: TestEncoderB -alpha -filter n -compress c -depth d -alpha means to use alpha encoding (default none) n is filter number 0=none (default), 1=sub, 2=up c is compression factor (0-9); 1 default d is pixel depth (8 or 24 bit); 24 default
You may see the source code for PngEncoder.java or PngEncoderB.java here. You may also download the entire package as a TAR-gzip file or as a .zip file. The API documentation is here.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Thanks to Jay Denny of KeyPoint Software, who let me develop this on company time.
Information from the PNG Specification was immensely useful in developing this code. The specification is also available in many forms and from many mirrors.